module dungeon.item;

import derelict.opengl.gl;

import nade.vector;
import nade.sprite;
import nade.ast;
import nade.loader;

import dungeon.game;
import dungeon.map;
import dungeon.map_object;
import dungeon.character;


interface IUsableItem {
	bool use(Character character);
}


class Item {
	this(Game game)
	{
		_game = game;
	}

	ItemObject drop(Map map, const Vector2i coord, int quantity)
	{
		return new ItemObject(map, coord, this, quantity);
	}

	bool use(Character character)
	{
		return false;
	}

	string id() { return _id; }
	string name() { return _name; }
	Sprite sprite() { return _sprite; }
	int weight() { return _weight; }
	int volume() { return _volume; }
	string description() { return _description; }

	protected {
		Game			_game;
		string			_id, _name;
		Sprite			_sprite;
		int				_weight, _volume;
		string			_description = "";
	}

	static void install()
	{
		Loader!(Item).add("name",
			(ref Item item, AST ast, Object[string] state)
			{
				item._id = ast[0].as!string;
				item._name = ast[1].as!string;
				item._game.addItem(item);
			}
		);

		Loader!(Item).add("sprite",
			(ref Item item, AST ast, Object[string] state) { item._sprite = new Sprite("Textures\\Sprites\\Items\\" ~ ast[0].as!string ~ ".bmp"); }
		);

		Loader!(Item).add("weight",
			(ref Item item, AST ast, Object[string] state) { item._weight = ast[0].as!int; }
		);

		Loader!(Item).add("volume",
			(ref Item item, AST ast, Object[string] state) { item._volume = ast[0].as!int; }
		);

		Loader!(Item).add("description",
			(ref Item item, AST ast, Object[string] state) { item._description = ast[0].as!string; }
		);
	}
}


class ItemObject : MapObject {
	this(Map map, const Vector2i coord, Item item, int quantity)
	{
		assert(item !is null);

		super(map, coord);

		_item = item;
		_quantity = quantity;
	}

	override void render()
	{
		glPushMatrix();
		glTranslatef(_coord.x + 0.5, _coord.y + 0.5, 0);
		_item.sprite.render(Vector2(0.8, 0.8));
		glPopMatrix();
	}

	Item item() { return _item; }
	int quantity() { return _quantity; }

	protected {
		Item		_item;
		int			_quantity;
	}
}
